package com.bbcare.exportExcel.plat.utils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * json字符串转化为Map或者List
 * 
 * @author xwc
 *
 */
public class Json2Map {

	/**
	 * json转化为List,首位需要有[]
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static List<Map<String, Object>> parseJSON2List(String jsonStr) {
		JSONArray jsonArr = JSONArray.fromObject(jsonStr);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		@SuppressWarnings("unchecked")
		Iterator<JSONObject> it = jsonArr.iterator();
		while (it.hasNext()) {
			JSONObject json2 = it.next();
			list.add(parseJSON2Map(json2.toString()));
		}
		return list;
	}

	/**
	 * json转化为Map,首尾不需要[]
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static Map<String, Object> parseJSON2Map(String jsonStr) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 最外层解析
		JSONObject json = JSONObject.fromObject(jsonStr);
		for (Object k : json.keySet()) {
			Object v = json.get(k);
			// 如果内层还是数组的话，继续解析
			if (v instanceof JSONArray) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				@SuppressWarnings("unchecked")
				Iterator<JSONObject> it = ((JSONArray) v).iterator();
				while (it.hasNext()) {
					JSONObject json2 = it.next();
					list.add(parseJSON2Map(json2.toString()));
				}
				map.put(k.toString(), list);
			} else {
				map.put(k.toString(), v);
			}
		}
		return map;
	}

	public static void main(String[] args) throws ParseException {
		/*String back = "{\"success\":true,\"msg\":null,\"data\":{"
				+ "\"babyList\":[{\"babyId\":\"297ebe0e4fb1d8e0014fb1f18b380007\","
				+ "\"babyName\":\"测试宝宝156\"," + "\"nickName\":null,"
				+ "\"gender\":\"F\"," + "\"birthday\":\"2015-09-02\","
				+ "\"iconPath\":null," + "\"channelId\":null,"
				+ "\"note\":null," + "\"groupId\":null,"
				+ "\"pregnancyWeek\":280," + "\"username\":null,"
				+ "\"password\":null," + "\"writetime\":1441799441000,"
				+ "\"health\":null," + "\"relationship\":null,"
				+ "\"card\":null," + "\"address\":null,"
				+ "\"ageweek\":\"1周\"," + "\"height\":null,"
				+ "\"weight\":null}],"
				+ "\"alias\":\"ee94326f-56e8-11e5-9b25-008cfae40c2c\"}}";
		Map<String, Object> map = Json2Map.parseJSON2Map(back);
		Map<String, Object> map2 = Json2Map.parseJSON2Map(map
				.get("data")
				.toString()
				.substring(map.get("data").toString().indexOf('[') + 1,
						map.get("data").toString().indexOf(']')));
		Subscriber subscriber = new Subscriber();
		subscriber.setBabyName(map2.get("babyName").toString());
		subscriber.setBabyId(map2.get("babyId").toString());
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
		subscriber.setAge(sdFormat.parse(map2.get("birthday").toString()));
		subscriber.setPregnantWeek(Integer.parseInt(map2.get("pregnancyWeek")
				.toString()));
		subscriber.setBabyGender(map2.get("gender").toString());
		subscriber.setIsCurrBaby(true);
		System.out.println(subscriber.getBabyName());
		System.out.println(subscriber.getBabyId());
		System.out.println(subscriber.getBabyGender());
		System.out.println(subscriber.getAge());*/
	}
}
